package com.xhu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhu.common.request.GetByMonthReq;
import com.xhu.common.request.Page;
import com.xhu.exceptions.CompeteException;
import com.xhu.pojo.Compete;
import com.xhu.service.CompeteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/compete")
public class CompeteController extends MyBaseController {

    @Autowired
    private CompeteService service;

    @PostMapping("/getalltopage")
    public String getAllToPage(@RequestBody Page page) {
        IPage<Compete> res = service.getAllToPage(page.getCurrent(), page.getSize());

        return this.returnPages(res);
    }

    @PostMapping("/getallbymonth")
    public String getAllByMonth(@RequestBody GetByMonthReq req) {
        IPage<Compete> res = service.getAllByMonthToPage(req.getCurrentPage(), req.getSize(), req.getStringTime());

        return this.returnPages(res);
    }

    @PostMapping("/update")
    public String updateOnById(@RequestBody Compete entity) {
        service.updateOneById(entity);

        return this.returnSuccessWithNoData("修改成功");
    }

    @PostMapping("/insert")
    public String insertOne(@RequestBody Compete entity) {
        service.insertOne(entity);
        return this.returnSuccessWithNoData("插入成功");

    }


    @PostMapping("/guidang")
    public String guidang(@RequestBody String id) {
        if (id == null) {
            throw new CompeteException("非法输入");
        }

        int realId = Integer.parseInt(this.getSingleStr(id, "id"));

        service.guidang(realId);
        return this.returnSuccessWithNoData("归档成功");

    }


}
